﻿using NB.MGJ.Common;
using NB.MGJ.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace NB.MGJ.Web.Controllers
{
    public partial class ProductController 
    {

        //-------------------------------------------产品分类start---------------------------------//

        #region 视图

        public ActionResult ProductType()
        {
            return View();
        }

        public ActionResult AddType()
        {
            return View(new Product_ProductTypes
            {
            });
        }

        public ActionResult EditType(int id)
        {
            Product_ProductTypes m = new Product_ProductTypes();
            m = _productService.GetProductType(id);
            return View(m);
        }


        #endregion

        #region 增

        public ActionResult AddProductType(L.EntityModel.Product_ProductTypes model)
        {
            if (model == null)
            {
                throw new Exception("当前无法执行该操作");
            }
            model.ChangeDate = DateTime.Now;
            model.ChangeLoginID = LogOnUser.UserID;
            model.ChangeLoginName = LogOnUser.LoginName;
            model.EnterpriseId = LogOnUser.EnterpriseId;
            _productService.AddProductType(model);
            return Json(true);
        }

        #endregion

        #region 删

        public ActionResult DeleteProductType(int id)
        {
            _productService.DeleteProductType(id);
            return Json(true);
        }

        #endregion

        #region 改

        public ActionResult EditProductType(L.EntityModel.Product_ProductTypes model)
        {
            if (model == null)
            {
                throw new Exception("当前无法执行该操作");
            }


            model.ChangeDate = DateTime.Now;
            model.ChangeLoginID = LogOnUser.UserID;
            model.ChangeLoginName = LogOnUser.LoginName;
            _productService.EditProductType(model);
            return Json(true);
        }

        #endregion

        #region 查

        public ActionResult GetProductTypeList(int pageindex = 1, int pageSize = 10, string prosearch = null)
        {
            var r = new PagerModel
            {
                PageSize = pageSize,
                Page = pageindex
            };
            var result = _productService.GetProductTypeList(prosearch, r, LogOnUser);
            return Json(result);
        }

        #endregion

        #region 导入导出Excle

        public ActionResult GetExecl(string key)
        {
            var model = _productService.Getproducttype(key);
            var _byte_batch = Excel.Exprot<Product_ProductTypes>(model.ToList().Where(p => p.ViewDel == 0).ToList(), new string[] { "类型名称", "所属机构", "备注", "创建时间" }, p =>
            {
                return new object[] { 
							p.TypeName,
							p.EnterpriseId,
							p.Remark,
							p.ChangeDate
				};
            }, "ProductType");
            return File(_byte_batch, "application/octet-stream", string.Format("{0}.xls", DateTime.Now.ToString("yyyyMMddHHmmssfff")));
        }

        #endregion




	}
}